package com.enjoyor.soa.traffic.server.efoms.dubbo.invoke;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.api.dto.idcs.VehiclePassDto;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class DubboSolrClientInvoke {

	@Value("${solr.client.url}")
	private String solrClientUrl;	
	
	private static DubboSolrClientInvoke instance;

	public static DubboSolrClientInvoke getInstance() {
		if (instance == null) {
			instance = new DubboSolrClientInvoke();
			//instance.initUrl();
			//instance.initType();
		}
		return instance;
	}
	
	@SuppressWarnings({ "unchecked"})
	public VehiclePassDto getRealVehSear(String bayonetId, String startTime, String endTime, String token){
		VehiclePassDto passDto = new VehiclePassDto();
		try {
			StringBuilder path = new StringBuilder(solrClientUrl).append("?token=").append(token);
			path.append("&pageSize=5&currentPage=1");
			if (bayonetId != null && !bayonetId.isEmpty()) {
				path.append("&pointIds=").append(URLEncoder.encode(bayonetId, "utf-8"));
			}
			if (startTime != null && !startTime.isEmpty()) {
				path.append("&startTime=").append(URLEncoder.encode(startTime, "utf-8"));
			}
			if (endTime != null && !endTime.isEmpty()) {
				path.append("&endTime=").append(URLEncoder.encode(endTime, "utf-8"));
			}
			URL restServiceURL = new URL(path.toString());
			HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
			httpConnection.setRequestMethod("GET");
			httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
			if (httpConnection.getResponseCode() != 200) {
				throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));
			StringBuffer sb = new StringBuffer();
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			ResultPojo result = JSONObject.parseObject(sb.toString(), ResultPojo.class);
			if (result.getAppCode().equals("0")) {
				List<JSONObject> list = (List<JSONObject>) ((JSONObject) result.getResultList()).get("result");
				if(list.size() > 0) {
					JSONObject obj = list.get(0);
					passDto.setBayonet_id(obj.getString("point_id"));//卡口编号
					//passDto.setBayonet_name(obj.getString("bayonet_name"));
					passDto.setId(obj.getString("id"));//ID
					passDto.setLane_id(obj.getString("lane_number"));//车道；
					if (obj.getString("speed_limit_max") != null && !obj.getString("speed_limit_max").equals(""))
						passDto.setMax_speed(Integer.parseInt(obj.getString("speed_limit_max")));//最大限速；
					if (obj.getString("speed_limit_min") != null && !obj.getString("speed_limit_min").equals(""))
						passDto.setMin_speed(Integer.parseInt(obj.getString("speed_limit_min")));//最低限速；
					if (obj.getString("speed") != null && !obj.getString("speed").equals(""))
						passDto.setSpeed(Integer.parseInt(obj.getString("speed")));//速度；
					passDto.setPass_id(obj.getString("id"));
					passDto.setPass_time(obj.getString("passing_time").replace("T", " ").replace("Z", ""));//过车时间；
					String pic_directory = obj.getString("pic_directory");
					String url = obj.getString("veh_pic");
					String pic_url1 = pic_directory+url;
					passDto.setPic_url1(obj.getString(pic_url1));//图片路径；
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return passDto;
		}
		return passDto;
	}
}
